import config from './gameconfig.js'

class Block {
  img: HTMLImageElement
  x: number
  y: number
  width: number
  height: number

  constructor(
    x: number = 0,
    y: number = 0,
    imgSrc: string | undefined = config.DefaltSprite,
    width: number = config.BlockSize,
    height: number = config.BlockSize,
  ) {
    this.img = new Image()
    this.img.src = imgSrc
    this.x = x
    this.y = y
    this.width = width
    this.height = height
  }

  collision(other: Block): boolean {
    if (
      Math.abs(this.x + this.width / 2 - (other.x + other.width / 2)) <
        Math.abs(this.width + other.width) / 2 - config.CollistionReduce &&
      Math.abs(this.y + this.height / 2 - (other.y + other.height / 2)) <
        Math.abs(this.height + other.height) / 2 - config.CollistionReduce
    ) {
      return true
    }
    return false
  }

  render(context: CanvasRenderingContext2D | null): void {
    if (!context) return
    context.drawImage(this.img, this.x, this.y, this.width, this.height)
  }
}

export default Block
